package ru.inse.web;

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import ru.inse.web.module.IInseModuleRegisterService;
import ru.inse.web.module.InseModule;
import ru.xeden.core.resource.BundleResourceSource;


public class Activator
    implements BundleActivator
{
    private XedenModuleRegisterServiceTracker moduleRegisterServiceTracker;
    ServiceRegistration<IInseModuleRegisterService> registerService;
    private InseModule module;
    private CityServiceTracker cityServiceTracker;

    
    public void start(BundleContext context)
        throws Exception
    {
        
        module = new InseModule(new BundleResourceSource(context));
        
        moduleRegisterServiceTracker = new XedenModuleRegisterServiceTracker(context, module);
        moduleRegisterServiceTracker.open();
        
        cityServiceTracker = new CityServiceTracker(context, module);
        cityServiceTracker.open();

        
        registerService = context.registerService(IInseModuleRegisterService.class, module, new Hashtable<String, Object>());
        
    }

    public void stop(BundleContext context)
        throws Exception
    {

        moduleRegisterServiceTracker.close();
        moduleRegisterServiceTracker = null;
        
        context.ungetService(registerService.getReference());
        
        module.dispose();
        module = null;

        cityServiceTracker.close();
        cityServiceTracker = null;
    }
    

}
